package com.hjn.tools;

import org.springframework.util.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author: huaijn
 * @create: 2022/4/14 21:42
 * @description: todo
 **/
public class PropertiesLoad {
    public static PropertiesLoad propertiesLoad;
    /*properties的文件路径*/
    private static String properties_Path = "/redis.properties";
    private static Properties properties;

    private PropertiesLoad() {
    }

    public static PropertiesLoad getInstance() {
        if (propertiesLoad == null) {
            propertiesLoad = new PropertiesLoad();
        }
        init();
        return propertiesLoad;
    }

    private static void init() {
        properties = new Properties();
        InputStream resourceAsStream = PropertiesLoad.class.getClassLoader().getResourceAsStream(properties_Path);
        try {
            properties.load(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getProperties(String key) {
        if (StringUtils.isEmpty(key)) {
            return null;
        }
        return properties.getProperty(key);
    }

    public static void main(String[] args) {
        PropertiesLoad instance = PropertiesLoad.getInstance();
        String properties = instance.getProperties("redis.url");
        System.out.println(properties);
    }
}
